package leetcode.code0284;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

public class PeekingIterator extends Solution284 implements Iterator<Integer> {

	private int[] nums;

	private int p;

	private int length;

	public PeekingIterator(Iterator<Integer> iterator) {
		length = 0;
		p = 0;
		Queue<Integer> list = new LinkedList<>();
		while (iterator.hasNext()) {
			list.add(iterator.next());
			length++;
		}
		nums = new int[length];
		for (int i = 0; i < length; i++) {
			nums[i] = list.poll();
		}
	}

	// Returns the next element in the iteration without advancing the iterator.
	public Integer peek() {
		return p < length ? nums[p] : null;
	}

	// hasNext() and next() should behave the same as in the Iterator interface.
	// Override them if needed.
	@Override
	public Integer next() {
		return p < length ? nums[p++] : null;
	}

	@Override
	public boolean hasNext() {
		return p < length;
	}

	public static void main(String[] args) {

	}

}
